package cn.iocoder.yudao.module.alipay.dal.mysql.babyorder;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.alipay.controller.app.babyorder.vo.BabyOrderExportReqVO;
import cn.iocoder.yudao.module.alipay.controller.app.babyorder.vo.BabyOrderPageReqVO;
import cn.iocoder.yudao.module.alipay.dal.dataobject.babyorder.BabyOrderDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.alipay.controller.app.babyorder.vo.*;

/**
 * 宝宝起名订单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface BabyOrderMapper extends BaseMapperX<BabyOrderDO> {

    default PageResult<BabyOrderDO> selectPage(BabyOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BabyOrderDO>()
                .eqIfPresent(BabyOrderDO::getOutTradeNo, reqVO.getOutTradeNo())
                .eqIfPresent(BabyOrderDO::getTradeNo, reqVO.getTradeNo())
                .eqIfPresent(BabyOrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(BabyOrderDO::getBuyerId, reqVO.getBuyerId())
                .eqIfPresent(BabyOrderDO::getAppId, reqVO.getAppId())
                .eqIfPresent(BabyOrderDO::getCode, reqVO.getCode())
                .eqIfPresent(BabyOrderDO::getMessage, reqVO.getMessage())
                .eqIfPresent(BabyOrderDO::getOrderStatues, reqVO.getOrderStatues())
                .betweenIfPresent(BabyOrderDO::getOrderTime, reqVO.getOrderTime())
                .eqIfPresent(BabyOrderDO::getTotalAmount, reqVO.getTotalAmount())
                .eqIfPresent(BabyOrderDO::getRetailPrise, reqVO.getRetailPrise())
                .eqIfPresent(BabyOrderDO::getCostPrise, reqVO.getCostPrise())
                .likeIfPresent(BabyOrderDO::getFamilyName, reqVO.getFamilyName())
                .eqIfPresent(BabyOrderDO::getSex, reqVO.getSex())
                .betweenIfPresent(BabyOrderDO::getSolarDate, reqVO.getSolarDate())
                .betweenIfPresent(BabyOrderDO::getLunarDate, reqVO.getLunarDate())
                .eqIfPresent(BabyOrderDO::getType, reqVO.getType())
                .eqIfPresent(BabyOrderDO::getForbid, reqVO.getForbid())
                .eqIfPresent(BabyOrderDO::getSubject, reqVO.getSubject())
                .eqIfPresent(BabyOrderDO::getCreator, reqVO.getCreator())
                .betweenIfPresent(BabyOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BabyOrderDO::getId));
    }

    default List<BabyOrderDO> selectList(BabyOrderExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BabyOrderDO>()
                .eqIfPresent(BabyOrderDO::getOutTradeNo, reqVO.getOutTradeNo())
                .eqIfPresent(BabyOrderDO::getTradeNo, reqVO.getTradeNo())
                .eqIfPresent(BabyOrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(BabyOrderDO::getBuyerId, reqVO.getBuyerId())
                .eqIfPresent(BabyOrderDO::getAppId, reqVO.getAppId())
                .eqIfPresent(BabyOrderDO::getCode, reqVO.getCode())
                .eqIfPresent(BabyOrderDO::getMessage, reqVO.getMessage())
                .eqIfPresent(BabyOrderDO::getOrderStatues, reqVO.getOrderStatues())
                .betweenIfPresent(BabyOrderDO::getOrderTime, reqVO.getOrderTime())
                .eqIfPresent(BabyOrderDO::getTotalAmount, reqVO.getTotalAmount())
                .eqIfPresent(BabyOrderDO::getRetailPrise, reqVO.getRetailPrise())
                .eqIfPresent(BabyOrderDO::getCostPrise, reqVO.getCostPrise())
                .likeIfPresent(BabyOrderDO::getFamilyName, reqVO.getFamilyName())
                .eqIfPresent(BabyOrderDO::getSex, reqVO.getSex())
                .betweenIfPresent(BabyOrderDO::getSolarDate, reqVO.getSolarDate())
                .betweenIfPresent(BabyOrderDO::getLunarDate, reqVO.getLunarDate())
                .eqIfPresent(BabyOrderDO::getType, reqVO.getType())
                .eqIfPresent(BabyOrderDO::getForbid, reqVO.getForbid())
                .eqIfPresent(BabyOrderDO::getSubject, reqVO.getSubject())
                .betweenIfPresent(BabyOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BabyOrderDO::getId));
    }

    /**
     * 待支付
     * 支付状态为0,30分钟以内
     * @param buyerId
     * @return
     */
    default List<BabyOrderDO> selectByBuyerId(String buyerId) {
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-30);
        return selectList(new LambdaQueryWrapperX<BabyOrderDO>()
                .eq(BabyOrderDO::getOrderStatues, "0")
                .eq(BabyOrderDO::getBuyerId, buyerId)
                .ge(BabyOrderDO::getCreateTime, LocalDateTime.now().plusMinutes(-30))
        );
    }

    default BabyOrderDO selectByTradeNo(String tradeNo) {
        return selectOne(new LambdaQueryWrapperX<BabyOrderDO>()
                .eq(BabyOrderDO::getOrderStatues, "1")
                .eq(BabyOrderDO::getTradeNo, tradeNo)
        );
    }



    /**
     * 失效订单
     * @param buyerId
     * @return
     */
    default List<BabyOrderDO> getBabyOrderListByBuyerId(String buyerId) {
        return selectList(new LambdaQueryWrapperX<BabyOrderDO>()
                .eq(BabyOrderDO::getOrderStatues, "0")
                .eq(BabyOrderDO::getBuyerId, buyerId)
                .le(BabyOrderDO::getCreateTime, LocalDateTime.now().plusMinutes(-30))
        );
    }

    /**
     * 无效订单
     * @param buyerId
     * @return
     */
    default List<BabyOrderDO> getInvalidBabyOrderListByBuyerId(String buyerId) {
        return selectList(new LambdaQueryWrapperX<BabyOrderDO>()
                .eq(BabyOrderDO::getOrderStatues, "3")
                .eq(BabyOrderDO::getBuyerId, buyerId)
        );
    }

    /**
     * 已支付订单
     * @param buyerId
     * @return
     */
    default List<BabyOrderDO> getSuccessBabyOrderListByBuyerId(String buyerId) {
        return selectList(new LambdaQueryWrapperX<BabyOrderDO>()
                .eq(BabyOrderDO::getOrderStatues, "1")
                .eq(BabyOrderDO::getBuyerId, buyerId)
        );
    }


}
